package com.sservice.service.service.impl;

import com.sservice.common.ExportUtil;
import com.sservice.service.service.ExportService;
import com.sservice.service.service.IOrderService;
import com.sservice.base.Utils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 导出实现类
 * Created by suweiming on 2017/10/5.
 */
@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private IOrderService iOrderService;


    public OutputStream exportExcel(String sheetName, List<Map> datas, ServletOutputStream outputStream) {

        // 创建一个workbook 对应一个excel应用文件
        XSSFWorkbook workBook = new XSSFWorkbook();
        // 在workbook中添加一个sheet,对应Excel文件中的sheet
        XSSFSheet sheet = workBook.createSheet(sheetName);
        ExportUtil exportUtil = new ExportUtil(workBook, sheet);
        XSSFCellStyle headStyle = exportUtil.getHeadStyle();
        XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();
        // 构建表头
        XSSFRow headRow = sheet.createRow(0);
        headRow.setHeightInPoints((short) 30);
        XSSFCell cell = null;
        if (datas.size() > 0) { //  订单数大于 0
            Map map0 = datas.get(0);
            Set set = map0.keySet();
            Iterator iterator = set.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                cell = headRow.createCell(i++);
                cell.setCellStyle(headStyle);
                cell.setCellValue(iterator.next().toString());
            }
            // 构建表体数据
            for (int j = 0; j < datas.size(); j++) {
                XSSFRow bodyRow = sheet.createRow(j + 1);
                Map data = datas.get(j);
                Iterator iterator1 = data.values().iterator();
                int k = 0;
                bodyRow.setHeightInPoints((short) 28);
                while (iterator1.hasNext()) {
                    cell = bodyRow.createCell(k++);
                    cell.setCellStyle(bodyStyle);
                    Object o = iterator1.next();
                    if (o == null) {
                        o = "";
                    }
                    cell.setCellValue(o.toString());
                }
            }
            for(int e = 0;e<set.size();e++){
                sheet.autoSizeColumn(e);
            }
        }

        try {
            workBook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void setResponseHeader(HttpServletResponse response, HttpServletRequest request, String fileName) {
        response.setContentType("application/binary;charset=UTF-8");
        fileName = Utils.processFileName(request, fileName);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + (new SimpleDateFormat("yyyy-MM-dd").format(new Date())) + ".xlsx\"");// 组装附件名称和格式
        response.setCharacterEncoding("UTF-8");
    }
}
